<template>
  <div class="ranking-page">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="header-left">
        <div class="logo" @click="router.push('/userBook')">
          <img src="https://lovechronicle.oss-cn-zhangjiakou.aliyuncs.com/book.jpg" alt="Logo" class="logo-img">
          <span class="logo-text">图书借阅系统</span>
        </div>
      </div>

      <div class="header-center">
        <el-input
          v-model="searchText"
          placeholder="搜索图书..."
          clearable
          @keyup.enter="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
          <template #append>
            <el-button @click="handleSearch">搜索</el-button>
          </template>
        </el-input>
      </div>

      <div class="header-right">
        <el-button class="back-button" @click="router.back()">
          <el-icon><ArrowLeft /></el-icon>
          返回
        </el-button>
      </div>
    </el-header>

    <!-- 排行榜内容 -->
    <div class="ranking-content">
      <div class="ranking-title">
        <h1>排行榜</h1>
      </div>

      <div class="book-list" v-loading="loading">
        <div v-for="(book, index) in books" :key="book.id" class="book-item">
          <div class="rank-number" :class="getRankClass((currentPage - 1) * pageSize + index + 1)">
            {{ (currentPage - 1) * pageSize + index + 1 }}
          </div>
          <div class="book-cover" @click="handleBookClick(book)">
            <img :src="book.coverUrl" :alt="book.title">
          </div>
          <div class="book-info">
            <h3 class="book-title" @click="handleBookClick(book)">{{ book.title }}</h3>
            <div class="book-meta">
              <span class="author">{{ book.author }}</span>
              <span class="category">{{ book.category }}</span>
              <span class="word-count">{{ book.wordCount }}万字</span>
            </div>
            <p class="book-description">{{ book.description }}</p>
            <div class="book-stats">
              <span class="hot-degree">{{ book.hotDegree }}热度</span>
              <span class="publish-date">{{ formatDate(book.publishDate) }}</span>
            </div>
          </div>
        </div>

        <!-- 分页器 -->
        <div class="pagination" v-if="total > 0">
          <el-pagination
            v-model:current-page="currentPage"
            :page-size="20"
            :total="total"
            layout="total, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getRankingList } from '@/api/user'
import { Search, ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()
const searchText = ref('')
const loading = ref(false)

// 分页相关
const currentPage = ref(1)
const pageSize = ref(20)
const total = ref(0)
const books = ref([])

// 格式化日期，只显示年月日
const formatDate = (dateStr: string) => {
  if (!dateStr) return ''
  return dateStr.split(' ')[0]
}

// 获取排行榜数据
const fetchRankingList = async () => {
  loading.value = true
  try {
    const res = await getRankingList({
      pageNumber: currentPage.value,
      pageSize: pageSize.value
    })
    if (res.code === 0) {
      books.value = res.data.results
      total.value = res.data.total
    } else {
      ElMessage.error(res.msg || '获取排行榜数据失败')
    }
  } catch (error) {
    console.error('获取排行榜数据失败:', error)
    ElMessage.error('获取排行榜数据失败')
  } finally {
    loading.value = false
  }
}

// 获取排名样式类
const getRankClass = (rank: number) => {
  if (rank <= 3) {
    return `rank-${rank}`
  }
  return ''
}

// 处理分页变化
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  fetchRankingList()
  // 快速滚动到顶部
  window.scrollTo({
    top: 0,
    behavior: 'instant'
  })
}

// 处理搜索
const handleSearch = () => {
  if (!searchText.value.trim()) {
    return
  }
  router.push({
    path: '/user/search-results',
    query: { keyword: searchText.value.trim() }
  })
}

// 处理图书点击
const handleBookClick = (book: any) => {
  router.push({
    path: '/user/book-detail',
    query: { id: book.id }
  })
}

onMounted(() => {
  fetchRankingList()
})
</script>

<style scoped>
.ranking-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 30px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  height: 70px !important;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  gap: 15px;
  cursor: pointer;
}

.logo-img {
  width: 50px;
  height: 50px;
  object-fit: contain;
}

.logo-text {
  font-size: 26px;
  font-weight: 600;
  color: #2c3e50;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}

.header-center {
  width: 450px;
}

.header-center :deep(.el-input__wrapper) {
  height: 40px;
}

.header-right {
  display: flex;
  align-items: center;
}

.back-button {
  padding: 8px 16px;
  background-color: #fff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  color: #666;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.3s ease;
}

.back-button:hover {
  background-color: #f5f5f5;
  color: #333;
}

.ranking-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  margin-top: 20px;
}

.ranking-title {
  padding: 20px 0;
  text-align: center;
}

.ranking-title h1 {
  font-size: 28px;
  color: #333;
  margin: 0;
}

.book-list {
  margin-top: 20px;
}

.book-item {
  display: flex;
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
  position: relative;
}

.rank-number {
  position: absolute;
  left: 0;
  top: 20px;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  color: #fff;
  border-radius: 4px;
  background-color: #d4b895;
}

.rank-1 { background-color: #ff4e4e; }
.rank-2 { background-color: #ff6f3d; }
.rank-3 { background-color: #ffc107; }

.book-cover {
  width: 120px;
  height: 160px;
  margin-right: 20px;
  margin-left: 30px;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.book-cover:hover {
  transform: scale(1.05);
}

.book-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.book-info {
  flex: 1;
}

.book-title {
  font-size: 18px;
  color: #333;
  margin: 0 0 10px 0;
  cursor: pointer;
  transition: color 0.3s ease;
}

.book-title:hover {
  color: #1e88e5;
}

.book-meta {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
}

.book-meta > span {
  margin-right: 15px;
}

.book-description {
  font-size: 14px;
  color: #999;
  margin: 10px 0;
  line-height: 1.5;
}

.book-stats {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}

.hot-degree {
  color: #1e88e5;
  font-weight: bold;
}

.publish-date {
  color: #999;
}

:deep(.el-tabs__item) {
  font-size: 16px;
  padding: 0 20px;
}

:deep(.el-tabs__active-bar) {
  background-color: #ffd04b;
}

:deep(.el-tabs__item.is-active) {
  color: #333;
  font-weight: bold;
}

.pagination {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}

:deep(.el-pagination) {
  padding: 20px 0;
  text-align: center;
}

:deep(.el-pagination .el-select .el-input) {
  width: 120px;
}
</style> 